package com.estore.tools;

import com.estore.model.Product;
import java.io.*;
import java.util.Map;

public class DataFileViewer {

    public static void main(String[] args) {
        String filePath = "products.dat";
        viewDataFile(filePath);
    }

    @SuppressWarnings("unchecked")
    public static void viewDataFile(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.println("文件不存在: " + filePath);
            return;
        }

        try (ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream(filePath))) {

            Map<String, Product> productData = (Map<String, Product>) ois.readObject();

            System.out.println("=== 商品数据文件内容 ===");
            System.out.println("文件: " + filePath);
            System.out.println("大小: " + file.length() + " 字节");
            System.out.println("商品数量: " + productData.size());
            System.out.println("------------------------");

            if (productData.isEmpty()) {
                System.out.println("没有商品数据");
            } else {
                productData.values().forEach(product -> {
                    System.out.printf("ID: %s | 名称: %s | 价格: ¥%.2f | 库存: %d%n",
                            product.getId(), product.getName(),
                            product.getPrice(), product.getStock());
                });
            }

        } catch (IOException | ClassNotFoundException e) {
            System.out.println("读取文件失败: " + e.getMessage());
        }
    }
}